import os

# 获取此文件所在目录的绝对路径
_basedir = os.path.abspath(os.path.dirname(__file__))

# 确保数据目录存在
data_dir = os.path.join(_basedir, 'data')
os.makedirs(data_dir, exist_ok=True)

# 数据库配置
DATABASE_URL = os.environ.get("DATABASE_URL", f"sqlite:///{os.path.join(data_dir, 'monitoring.db')}")

# 服务器配置
HOST = os.environ.get("HOST", "0.0.0.0")
PORT = int(os.environ.get("PORT", 8000))
DEBUG = os.environ.get("DEBUG", "true").lower() == "true"

# 日志配置
LOG_LEVEL = os.environ.get("LOG_LEVEL", "INFO")

# SSH默认配置
DEFAULT_SSH_PORT = 22
DEFAULT_SSH_USER = "root"


METRIC_COLLECTION = {
    # "once": {
    #     "info": {
    #         "command": "uname -a",
    #         "interval": -1,
    #         "parser": "parse_uname"
    #     }
    # },
    "cpu": {
        "nproc": {
            "command": "nproc",
            "interval": -1,
            "parser": "none"
        },
        "loadavg": {
            "command": "cat /proc/loadavg",
            "interval": 10,
            "parser": "parse_loadavg"
        },
        "syscalls_per_sec": {
            "command": "perf stat -e 'syscalls:*' -a sleep 1 2>&1 | grep syscalls | wc -l",
            "interval": 15,
            "parser": "none"
        },
        "cpu_usage": {
            "command": "mpstat -P ALL 1 1 | grep 'Average' | awk '{print $3, $5, $6, $12}'",
            "interval": 10,
            "parser": "parse_mpstat"
        },
        "process_count": {
            "command": "ps aux | wc -l",
            "interval": 15,
            "parser": "none"
        },
        "context_switches_interrupts": {
            "command": "vmstat 1 1 | awk 'NR>1 {print $11, $12}'",
            "interval": 10,
            "parser": "parse_vmstat_cs_in"
        },
        "ps_state": {
            "command": "ps -eo pid,ppid,user,%cpu,%mem,vsz,rss,stat,comm,time,start",
            "interval": 60,
            "parser": "parse_ps_state"
        }
        # "interrupts": {
        #     "command": "cat /proc/interrupts",
        #     "interval": 10,
        #     "parser": "parse_interrupts"
        # },
        # "softirqs": {
        #     "command": "cat /proc/softirqs",
        #     "interval": 10,
        #     "parser": "parse_softirqs"
        # }
    },
    "memory": {
        "info": {
            "command": "free -b",
            "interval": 10,
            "parser": "parse_free"
        },
        # "mem_usage_percent": {
        #     "command": "sar -r 1 1",
        #     "interval": 10,
        #     "parser": "parse_sar_mem"
        # },
        "oom_kill": {
            "command": "journalctl --since='1 hour ago' --grep='killed process' | grep -v '^--' | wc -l",
            "interval": 10,
            "parser": "none"
        },
        "swappiness": {
            "command": "sysctl vm.swappiness",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "page_scan": {
            "command": "cat /proc/vmstat | grep -E \"pgscan_kswapd|pgscan_direct\"; sleep 1; cat /proc/vmstat | grep -E \"pgscan_kswapd|pgscan_direct\"",
            "interval": 10,
            "parser": "parse_vmstat_page_scan"
        },
        "vfs_cache_pressure": {
            "command": "sysctl vm.vfs_cache_pressure",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "dirty_background_ratio": {
            "command": "sysctl vm.dirty_background_ratio",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "dirty_ratio": {
            "command": "sysctl vm.dirty_ratio",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "transparent_hugepage": { # always [madvise] never
            "command": "cat /sys/kernel/mm/transparent_hugepage/enabled | grep -o \\\\[.*\\\\] | tr -d []",
            "interval": -1,
            "parser": "none"
        }
        # "buddyinfo": {
        #     "command": "cat /proc/buddyinfo",
        #     "interval": 60,
        #     "parser": "parse_buddyinfo"
        # }
    },
    "io": {
        "device_stats": {
            "command": "iostat -dx 1 1 | awk 'NR>2'",
            "interval": 10,
            "parser": "parse_iostat"
        },
        "device_size": {
            "command": "lsblk -b -n -o NAME,SIZE,TYPE,FSUSED,FSUSE% || lsblk -bd -n -o NAME,SIZE,TYPE",
            "interval": -1,
            "parser": "parse_lsblk"
        },
        "device_sched_queDepth_rdAhead": {
            "command": "for dev in $(lsblk -d -n -o NAME); do echo -n \"$dev \"; awk '{printf \"%s \", $1}' /sys/block/$dev/queue/scheduler /sys/block/$dev/queue/nr_requests /sys/block/$dev/queue/read_ahead_kb; echo; done",
            "interval": -1,
            "parser": "parse_sched_queDepth_rdAhead"
        },
        "inode_usage": {
            "command": "df -i | grep '^/dev/'",
            "interval": 300,
            "parser": "parse_df_i"
        },
        "fs_space": {
            "command": "df -TP | grep '^/dev/'",
            "interval": -1,
            "parser": "parse_df_TP"
        }
    },
    "network": {
        "net_stats": {
            "command": "sar -n DEV 1 1 | grep Average",
            "interval": 10,
            "parser": "parse_sar_dev"
        },
        "net_err_stats": {
            "command": "sar -n EDEV 1 1 | grep Average",
            "interval": 10,
            "parser": "parse_sar_edev"
        },
        "net_speed_duplex_mtu": {
            "command": "for iface in $(ls /sys/class/net); do speed_duplex=$(ethtool $iface | grep -E 'Speed|Duplex' | awk '{printf \"%s \", $2}' ); mtu=$(cat /sys/class/net/$iface/mtu); echo \"$iface $speed_duplex$mtu\"; done",
            "interval": 10,
            "parser": "parse_speed_duplex_mtu"
        },
        "tcp_drops": {
            "command": "cat /proc/net/netstat | awk 'NR<3 {print $2, $20, $21}'",
            "interval": 10,
            "parser": "parse_netstat_drops"
        },
        "tcp_retrans": {
            "command": "netstat -s | grep -i 'segments retransmit'; sleep 1; netstat -s | grep -i 'segments retransmit'",
            "interval": 15,
            "parser": "parse_netstat_retrans"
        },
        "tcp_congestion_control": {
            "command": "sysctl net.ipv4.tcp_congestion_control",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "tcp_rmem": {
            "command": "sysctl net.ipv4.tcp_rmem",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "tcp_wmem": {
            "command": "sysctl net.ipv4.tcp_wmem",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "tcp_max_syn_backlog": {
            "command": "sysctl net.ipv4.tcp_max_syn_backlog",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "somaxconn": {
            "command": "sysctl net.core.somaxconn",
            "interval": -1,
            "parser": "parse_sysctl"
        },
        "tcp_window_scaling": {
            "command": "sysctl net.ipv4.tcp_window_scaling",
            "interval": -1,
            "parser": "parse_sysctl"
        }
    },
    "mysql": {
        "qps": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Queries\";' | awk 'NR==2{print $2}'; sleep 1; mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Queries\";' | awk 'NR==2{print $2}'",
            "interval": 10,
            "parser": "parse_rate"
        },
        "tps": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Com_commit\";' | awk 'NR==2{print $2}'; sleep 1; mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Com_commit\";' | awk 'NR==2{print $2}'",
            "interval": 10,
            "parser": "parse_rate"
        },
        "slow_queries": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Slow_queries\";' | awk 'NR==2{print $2}'",
            "interval": 60,
            "parser": "none"
        },
        "threads_connected": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Threads_connected\";' | awk 'NR==2{print $2}'",
            "interval": 10,
            "parser": "none"
        },
        "threads_running": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Threads_running\";' | awk 'NR==2{print $2}'",
            "interval": 10,
            "parser": "none"
        },
        "aborted_connects": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Aborted_connects\";' | awk 'NR==2{print $2}'",
            "interval": 60,
            "parser": "none"
        },
        "innodb_buffer_pool_reads": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Innodb_buffer_pool_reads\";' | awk 'NR==2{print $2}'",
            "interval": 60,
            "parser": "none"
        },
        "innodb_buffer_pool_read_requests": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Innodb_buffer_pool_read_requests\";' | awk 'NR==2{print $2}'",
            "interval": 60,
            "parser": "none"
        },
        "innodb_row_locks_waited": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Innodb_row_lock_waits\";' | awk 'NR==2{print $2}'",
            "interval": 60,
            "parser": "none"
        },
        "innodb_row_lock_time": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Innodb_row_lock_time\";' | awk 'NR==2{print $2}'",
            "interval": 60,
            "parser": "none"
        },
        "innodb_log_waits": {
            "command": "mysql -u{user} -p{password} -P{port} -e 'SHOW GLOBAL STATUS LIKE \"Innodb_log_waits\";' | awk 'NR==2{print $2}'",
            "interval": 60,
            "parser": "none"
        }
    }
}
